package org.sky.wall.visual.ui

import android.annotation.SuppressLint
import android.app.WallpaperManager
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.Gravity
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import org.sky.wall.visual.MainActivity
import org.sky.wall.visual.R
import org.sky.wall.visual.databinding.ActivityVideoBinding
import java.io.IOException

class VideoActivity : AppCompatActivity() {
	private lateinit var bin: ActivityVideoBinding
	private lateinit var getContentLauncher: ActivityResultLauncher<String>
	private var bitmap: Bitmap? = null
	
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		bin = ActivityVideoBinding.inflate(layoutInflater)
		setContentView(bin.root)
		getContentLauncher = registerForActivityResult(ActivityResultContracts.GetContent()) { imageUri: Uri? ->
			imageUri?.let { displayImage(it) }
		}
		initUri()
		setWallpaper()
	}
	private fun setWallpaper() {
		bin.setting.setOnClickListener {
			showWallpaperOptionsDialog()
		}
	}
	private fun initUri() {
		openGalleryForImage()
	}
	
	private fun showToast(message: String) {
		runOnUiThread {
			Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
			startActivity(Intent(this,MainActivity::class.java))
			finish()
		}
	}
	
	@SuppressLint("ResourceType")
	private fun showWallpaperOptionsDialog() {
		val builder: AlertDialog.Builder = AlertDialog.Builder(this)
		
		// Inflate custom layout
		val inflater = layoutInflater
		val customLayout = inflater.inflate(R.layout.setting_layout, null)
		
		builder.setView(customLayout)
		
		val wallpaperManager = WallpaperManager.getInstance(applicationContext)
		
		val bitmapDrawable = bin.img.drawable as? BitmapDrawable
		val bitmap = bitmapDrawable?.bitmap
		
		builder.setCancelable(true)
		
		val dialog: AlertDialog = builder.create()
		dialog.window?.setGravity(Gravity.BOTTOM)
		
		// Access the views in your custom layout
		val wholeOption = customLayout.findViewById<TextView>(R.id.whole)
		val desktopOption = customLayout.findViewById<TextView>(R.id.desktop)
		val lockOption = customLayout.findViewById<TextView>(R.id.lock)
		
		lockOption.setOnClickListener {
			// Handle Whole option in a background thread
			dialog.dismiss()
			Thread {
				setWallpaperWithFlag(wallpaperManager, bitmap, WallpaperManager.FLAG_LOCK)
				runOnUiThread {
					showToast("Successfully set")
				}
			}.start()
		}
		
		desktopOption.setOnClickListener {
			dialog.dismiss()
			// Handle Desktop option in a background thread
			Thread {
				setWallpaperWithFlag(wallpaperManager, bitmap, WallpaperManager.FLAG_SYSTEM)
				runOnUiThread {
					showToast("Successfully set")
				}
			}.start()
		}
		
		wholeOption.setOnClickListener {
			// Handle Lockscreen option in a background thread
			Thread {
				try {
					//wallpaperManager.setBitmap(bitmap)
					dialog.dismiss()
					bitmap?.let {
						wallpaperManager.setBitmap(it, null, true, WallpaperManager.FLAG_LOCK or WallpaperManager.FLAG_SYSTEM)
					}
					runOnUiThread {
						showToast("Successfully set")
					}
				} catch (e: IOException) {
					e.printStackTrace()
				}
			}.start()
		}
		
		dialog.show()
	}
	
	
	
	private fun setWallpaperWithFlag(
			wallpaperManager: WallpaperManager,
			bitmap: Bitmap?,
			flag: Int
	) {
		bitmap?.let {
			try {
				wallpaperManager.setBitmap(it, null, true, flag)
			
			} catch (e: IOException) {
				e.printStackTrace()
			}
		}
	}
	private fun displayImage(url: Uri) {
		bin.img.setImageURI(url)
		try {
			// 将选择的图片转换为 Bitmap
			 bitmap = MediaStore.Images.Media.getBitmap(contentResolver, url)
		} catch (e: IOException) {
			e.printStackTrace()
		}
	}
	
	private fun openGalleryForImage() {
		// 启动内容选择器，选择图片
		getContentLauncher.launch("image/*") // 修改参数为 "image/*"
	}
	
}